<template>
  <div class="back_img">
    <div class="background">
      <div class="login">

        <el-form  ref="registerForm" :model="registerForm" :rules="registerRules" status-icon>
          <div class="login_title">
            <h1 class="bigtitle">注 册</h1>
          </div>
          <div class="login_input">
            <el-form-item prop="name">
              <el-input v-model="registerForm.username" auto-complete="off" placeholder="请输入用户名"
                        prefix-icon="el-icon-user" type="text"
              ></el-input>
            </el-form-item>
          </div>
          <div class="login_input">
            <el-form-item prop="password">
              <el-input v-model="registerForm.password" auto-complete="off" placeholder="请输入密码"
                        prefix-icon="el-icon-unlock" type="password" @keyup.enter.native="handleRegister"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="registerForm.confirmPassword" auto-complete="off" placeholder="请再次输入密码"
                        prefix-icon="el-icon-unlock" type="password" @keyup.enter.native="handleRegister"
              ></el-input>
            </el-form-item>
          </div>

          <el-form-item>
            <el-button  :loading="loading" class="homeBut" size="mini" type="primary" @click.native.prevent="handleRegister">
              确定注册
            </el-button>
            <el-button class="homeBut" size="mini" type="primary" @click="tocmsLogin">
              去登陆
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="item">
        <div class="text">
          <h1 style="font-size: 30px">欢迎回来</h1>
          <h3>无论才能、知识多么卓越，如果缺乏热情，则无异于纸上画饼充饥，无补于事</h3>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaOnOff: true
    };
  },
  created() {
  },
  methods: {
    tocmsLogin(){
      this.$router.push({path: '/cmsLogin'})
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/cmsLogin");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style scoped>

body {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

.background {
  height: 400px;
  width: 620px;
  margin: 130px auto 0;
  box-shadow: 0 5px 9px rgba(.80, 0, 0, .80), 0 0 10px rgba(0, 0, 0, .80)
}

.item {
  width: 300px;
  height: 400px;

  float: right;
  margin: 0 0 0 0;
  padding: 10px 25px;

}

.text {
  text-align: center;
  margin-top: 40%;
  color: #ececec;
  font-family: STXinwei;
}


.homeBut {

  width: 100px;
  margin-bottom: 5px;
  margin-top: 18px;
  margin-left: 6.5%;

}

.login {
  width: 300px;
  height: 400px;

  float: right;
  margin: 0 0 0 0;


  padding: 10px 25px;


}

.login_title {
  text-align: center;
  margin-top: 20px;
}


.back_img {
  background: url("../../assets/images/wallhaven-k9p8l6.webp");
  background-size: 100% 100%;
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}

.bigtitle {
  font-family: "STXingkai";
  font-size: 45px;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
